home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 3 / Amiga Tools 3.iso / grafik / raytracing / rayshade-4.0.6.3 / urt / amiga_extras / avg2 / avg2.c next >
Encoding:
C/C++ Source or Header  |  1991-08-09  |  4.1 KB  |  152 lines

  1. /*
  2.  * This software is copyrighted as noted below.  It may be freely copied,
  3.  * modified, and redistributed, provided that the copyright notice is 
  4.  * preserved on all copies.
  5.  * 
  6.  * There is no warranty or other guarantee of fitness for this software,
  7.  * it is provided solely "as is".  Bug reports or fixes may be sent
  8.  * to the author, who may or may not act on them as he desires.
  9.  *
  10.  * You may not include this software in a program or other software product
  11.  * without supplying the source, or without informing the end-user that the 
  12.  * source is available for no extra charge.
  13.  *
  14.  * If you modify this software, you should include a notice giving the
  15.  * name of the person performing the modification, the date of modification,
  16.  * and the reason for such modification.
  17.  */
  18. /* 
  19.  * avg2.c - Reduce image by half in X, producing alphas even
  20.  *          if they weren't there originally.
  21.  * 
  22.  * Author:    Kriton Kyrimis
  23.  *    Based on avg4 by:
  24.  *        Rod Bogart & John W. Peterson
  25.  *         Computer Science Dept.
  26.  *         University of Utah
  27.  * Date:    Fri Jun 20 1986
  28.  * Copyright (c) 1986, University of Utah
  29.  * 
  30.  */
  31. #ifndef lint
  32. static char rcs_ident[] = "$Header: /usr/users/spencer/src/urt/tools/RCS/avg2.c,v 3.0 90/08/03 15:21:41 spencer Exp $";
  33. #endif
  34.  
  35.  
  36. #include <stdio.h>
  37. #include <rle.h>
  38. #ifdef USE_STDLIB_H
  39. #include <stdlib.h>
  40. #endif /* USE_STDLIB_H */
  41.  
  42. #define MALLOC_ERR {fprintf(stderr, "avg2: ran out of heap space\n");exit(-2);}
  43.  
  44. static bit_count[16] = {0, 63, 63, 127, 63, 127, 127,
  45.     192, 63, 127, 127, 192, 127, 192, 192, 255};
  46.  
  47. void
  48. main(argc, argv)
  49. int    argc;
  50. char    *argv[];
  51. {
  52.     char    *infname = NULL, *outfname = NULL;
  53.     char    *cmd = cmd_name( argv );
  54.     int        oflag = 0;
  55.     int        rle_cnt;
  56.     FILE    *outfile = stdout;
  57.     int         i, j;
  58.     int        new_xlen,
  59.         new_ylen;
  60.     int        rle_err;
  61.     rle_hdr in_hdr, out_hdr;
  62.     rle_pixel **rows0, **rowsout;
  63.     rle_pixel *ptr0, *ptrout, *alphptr;
  64.     int        A, chan;
  65.     
  66.     if ( scanargs( argc, argv, "% o%-outfile!s infile%s",
  67.            &oflag, &outfname, &infname ) == 0 )
  68.     exit( 1 );
  69.  
  70.     in_hdr.rle_file = rle_open_f( cmd, infname, "r" );
  71.  
  72.     for ( rle_cnt = 0;
  73.       (rle_err = rle_get_setup( &in_hdr )) == RLE_SUCCESS;
  74.       rle_cnt++ )
  75.     {
  76.     if ( rle_cnt == 0 )
  77.         outfile = rle_open_f( cmd, outfname, "w" );
  78.  
  79.     out_hdr = in_hdr;
  80.     rle_addhist( argv, &in_hdr, &out_hdr );
  81.  
  82.     new_xlen = (in_hdr.xmax - in_hdr.xmin +1 ) / 2;
  83.     new_ylen = (in_hdr.ymax - in_hdr.ymin +1 );
  84.     out_hdr.xmin = in_hdr.xmin / 2;
  85.     out_hdr.ymin = in_hdr.ymin;
  86.     out_hdr.xmax = out_hdr.xmin + new_xlen - 1;
  87.     out_hdr.ymax = out_hdr.ymin + new_ylen - 1;
  88.  
  89.     out_hdr.alpha = 1;    /* Force alpha in output. */
  90.     RLE_SET_BIT( out_hdr, RLE_ALPHA );
  91.  
  92.     out_hdr.rle_file = outfile;
  93.     rle_put_setup( &out_hdr );
  94.  
  95.     /* Oink. */
  96.     if (rle_row_alloc( &in_hdr, &rows0 ))
  97.         MALLOC_ERR;
  98.  
  99.     if (rle_row_alloc( &out_hdr, &rowsout ))
  100.         MALLOC_ERR;
  101.  
  102.     for ( j = 0; j < new_ylen*2; j+=2 )
  103.     {
  104.         rle_getrow(&in_hdr, rows0 );
  105.  
  106.         for (chan = RLE_ALPHA; chan < in_hdr.ncolors; chan++)
  107.         {
  108.         ptr0 = &(rows0[chan][in_hdr.xmin]);
  109.         ptrout = rowsout[chan];
  110.         alphptr = rowsout[RLE_ALPHA];
  111.         /*
  112.          * If we don't start out with an alpha channel in the
  113.          * original image, then we want to fake one up.  This
  114.          * works by counting the number of non-zero pixels in the
  115.          * R, G and B channels.  We set bits in the alpha channel
  116.          * for the non-zero pixels found, then use bit_count to
  117.          * convert this to reasonable coverage values.
  118.          */
  119.         if ((chan == RLE_ALPHA) && (!in_hdr.alpha))
  120.         {
  121.             bzero(alphptr, new_xlen);
  122.         }
  123.         else for( i = 0; i < new_xlen; i++)
  124.         {
  125.             if (!in_hdr.alpha)
  126.             {
  127.             *alphptr |= (*ptr0 ? 1 : 0) | (ptr0[1] ? 2 : 0);
  128.  
  129.             /* calc fake alpha from bit count */
  130.             if (chan == (in_hdr.ncolors - 1))
  131.                 *alphptr = bit_count[*alphptr];
  132.  
  133.             alphptr++;
  134.             }
  135.             A = (int) *ptr0++ + (int) *ptr0++;
  136.             *ptrout++ = (rle_pixel) (A / 2);
  137.         }
  138.         }
  139.         rle_putrow( rowsout, new_xlen, &out_hdr );
  140.     }
  141.     rle_puteof( &out_hdr );
  142.  
  143.     rle_row_free( &in_hdr, rows0 );
  144.     rle_row_free( &out_hdr, rowsout );
  145.     }
  146.  
  147.     if ( rle_cnt == 0 || (rle_err != RLE_EOF && rle_err != RLE_EMPTY) )
  148.     rle_get_error( rle_err, cmd, infname );
  149.     
  150.     exit( 0 );
  151. }
  152.